package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.net.bean.GetBillListBean;

import java.util.ArrayList;
import java.util.List;

public class TransactionListAdapter extends RecyclerView.Adapter<TransactionListAdapter.Holder> {

    private final LayoutInflater inflater;
    private ArrayList<GetBillListBean.DataBean.BillListBean> data;
    private Context context;

    public TransactionListAdapter(Context context) {
        data = new ArrayList<>();
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void addData(List<GetBillListBean.DataBean.BillListBean> bean, boolean isRefresh) {
        if (isRefresh) {
            data.clear();
        }
        data.addAll(bean);
        notifyDataSetChanged();
    }

    public void setDataWithChangePosition(ArrayList<GetBillListBean.DataBean.BillListBean> bean, int changePosition) {
        this.data = bean;
        notifyItemChanged(changePosition);
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_transaction, parent, false);
        return new TransactionListAdapter.Holder(view);
    }

    @Override
    public void onBindViewHolder(TransactionListAdapter.Holder holder, final int position) {
        GetBillListBean.DataBean.BillListBean bean = data.get(position);

        holder.name.setText(bean.getContent());
        holder.date.setText(bean.getAdd_time());
        holder.tvWithdrawErrorDes.setVisibility(View.GONE);
        holder.tvWithdrawState.setVisibility(View.VISIBLE);
        if ("0".equals(bean.getIn_out())) {
            holder.change.setTextColor(Color.parseColor("#509ff8"));
            holder.change.setText("+ " + bean.getUser_money());
        } else {
            holder.change.setTextColor(Color.parseColor("#333333"));
            holder.change.setText("- " + bean.getUser_money());
        }
        if ("6".equals(bean.getBill_type())) {
            holder.tvWithdrawState.setVisibility(View.VISIBLE);
            if ("4".equals(bean.getStatus()) || "2".equals(bean.getStatus())) {
                if ("4".equals(bean.getStatus())) {
                    holder.tvWithdrawState.setText("转账失败");
                } else {
                    holder.tvWithdrawState.setText("审核失败");
                }
                holder.tvWithdrawState.setTextColor(Color.parseColor("#e65757"));
                holder.tvWithdrawErrorDes.setText(bean.getError_msg());
                if (bean.isTurnShow()) {
                    holder.tvWithdrawErrorDes.setVisibility(View.VISIBLE);
                } else {
                    holder.tvWithdrawErrorDes.setVisibility(View.GONE);
                }
            } else {
                holder.tvWithdrawState.setTextColor(Color.parseColor("#999999"));
                if ("0".equals(bean.getStatus())) {
                    holder.tvWithdrawState.setText("待审核");
                } else if ("1".equals(bean.getStatus())) {
                    holder.tvWithdrawState.setText("审核成功");
                } else if ("3".equals(bean.getStatus())) {
                    holder.tvWithdrawState.setText("转账成功");
                } else if ("5".equals(bean.getStatus())) {
                    holder.tvWithdrawState.setText("处理中");
                }
            }
        } else {
            holder.tvWithdrawState.setVisibility(View.GONE);
        }
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(data, position);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    static class Holder extends RecyclerView.ViewHolder {

        private final TextView name;
        private final TextView change;
        private final TextView date;
        private final TextView tvWithdrawState;
        private final TextView tvWithdrawErrorDes;

        public Holder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.tv_name);
            date = (TextView) itemView.findViewById(R.id.tv_date);
            change = (TextView) itemView.findViewById(R.id.tv_change);
            tvWithdrawState = (TextView) itemView.findViewById(R.id.tv_withdraw_state);
            tvWithdrawErrorDes = (TextView) itemView.findViewById(R.id.tv_withdraw_error_des);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(ArrayList<GetBillListBean.DataBean.BillListBean> data, int position);
    }

}
